package com.xyz.messagehelper.ui.activity

import android.support.v7.app.AlertDialog
import android.view.KeyEvent
import com.xyz.messagehelper.R
import com.xyz.messagehelper.bean.ConstantValue.PARSE_MESSAGE_FILE
import com.xyz.messagehelper.bean.MessageEvent
import com.xyz.messagehelper.utils.ActivityConctroler
import com.xyz.messagehelper.utils.SharePreferenceUtils
import kotlinx.android.synthetic.main.activity_message_rule_set.*
import kotlinx.android.synthetic.main.layout_toolbar.*
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast
import java.lang.ref.WeakReference


class MessageRulesSetActivity : BaseActivity() {

    override fun getLayoudResId(): Int = R.layout.activity_message_rule_set


    override fun init() {
        super.init()
        ActivityConctroler.add(WeakReference(this))
        setToolBar()
        initData()
    }

    private fun initData() {
        val string = SharePreferenceUtils.getString(this, "message_rules", null)
        val timeInterval = SharePreferenceUtils.getString(this, "time_interval", null)
        if (!timeInterval.isNullOrEmpty()) {
            etTimeNum.setText(timeInterval)
        }
        if (!string.isNullOrEmpty()) {
            etMessageTemplate.setText(string)
        }
        btnClearContent.setOnClickListener {
            etMessageTemplate.setText("")
            toast("请注意保存模板")
        }
        btnConfirme.setOnClickListener {
            val message = etMessageTemplate.text.toString().trim()
            val timeStr = etTimeNum.text.trim().toString()
            if (message.isEmpty() || timeStr.isEmpty()) {
                toast("填入的时间或者短信模板不能为空")
                return@setOnClickListener
            }
            SharePreferenceUtils.putString(this, "message_rules", message);
            SharePreferenceUtils.putString(
                this@MessageRulesSetActivity,
                "time_interval",
                timeStr
            )
            toast("已经保存保存配置")
        }
        btnSetDate.setOnClickListener {
            etMessageTemplate.append("{date}")
        }
        btnSetTime.setOnClickListener {
            etMessageTemplate.append("{time}")
        }
        btnExtendPara1.setOnClickListener {
            etMessageTemplate.append("{var1}")
        }
        btnExtendPara2.setOnClickListener {
            etMessageTemplate.append("{var2}")
        }
        btnExtendPara3.setOnClickListener {
            etMessageTemplate.append("{var3}")
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            showRemindDialog()
        }
        return super.onKeyDown(keyCode, event)
    }

    private fun setToolBar() {
        tv_title.text = "短信模板配置"
        iv_back.setOnClickListener {
            showRemindDialog()
        }
    }

    private fun showRemindDialog() {
        AlertDialog.Builder(this@MessageRulesSetActivity)
            .setTitle("提示")
            .setNegativeButton("取消保存") { _, _ ->
                run {
                    toast("取消保存")
                    startActivity<HomeActivity>()
                    EventBus.getDefault().postSticky(MessageEvent(PARSE_MESSAGE_FILE))
                    finish()
                }
            }
            .setPositiveButton("保存") { _, _ ->
                run {
                    val message = etMessageTemplate.text.toString().trim()
                    val timeStr = etTimeNum.text.trim().toString()
                    SharePreferenceUtils.putString(this, "message_rules", message);
                    SharePreferenceUtils.putString(
                        this@MessageRulesSetActivity,
                        "time_interval",
                        timeStr
                    )
                    toast("保存成功")
                    startActivity<HomeActivity>()
                    EventBus.getDefault().postSticky(MessageEvent(PARSE_MESSAGE_FILE))
                    finish()
                }
            }
            .setMessage("请您确认是否保存当前编辑的短信模板信息").create().show()
    }


}